package com.fin.quant.domain.service.impl;

import com.fin.quant.domain.service.DividendDaoService;
import com.fin.quant.instra.mysql.mapper.DividendHistoryMapper;
import com.fin.quant.instra.mysql.mapper.DividendMapper;
import com.fin.quant.instra.mysql.model.Dividend;
import com.fin.quant.instra.mysql.model.DividendExample;
import com.fin.quant.instra.mysql.model.DividendHistory;
import com.fin.quant.instra.mysql.model.DividendHistoryExample;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DividendDaoServiceImpl implements DividendDaoService {

    @Autowired
    private DividendHistoryMapper dividendHistoryMapper;

    @Autowired
    private DividendMapper dividendMapper;

    @Override
    public List<Dividend> findRecommendAsset(){
        DividendExample example = new DividendExample();
        example.setOrderByClause("dividend_yield desc limit 10");
        List<Dividend> dividends = dividendMapper.selectByExample(example);
        return dividends;
    }

    @Override
    public Dividend findDividendByAssetId(long assetId) {
        DividendExample example = new DividendExample();
        example.createCriteria().andAssetIdEqualTo(assetId);
        List<Dividend> dividends = dividendMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(dividends) ? dividends.get(0) : null;
    }

    @Override
    public List<Dividend> findDividendByAssetIdList(List<Long> assetIdList) {
        DividendExample example = new DividendExample();
        example.createCriteria().andAssetIdIn(assetIdList);
        return dividendMapper.selectByExample(example);
    }

    @Override
    public List<DividendHistory> findDividendHisList(List<Long> assetIdList) {
        DividendHistoryExample example = new DividendHistoryExample();
        example.createCriteria().andAssetIdIn(assetIdList);
        return dividendHistoryMapper.selectByExample(example);
    }

    @Override
    public int updateDividend(Dividend dividend) {
        return dividendMapper.updateByPrimaryKeySelective(dividend);
    }

    @Override
    public void saveDividend(Dividend dividend) {
        dividendMapper.insert(dividend);
    }

    @Override
    public void save(List<DividendHistory> list) {
        dividendHistoryMapper.batchInsertOrUpdate(list);
    }


}
